﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace MyQQ.Net
{
    /// <summary>
    /// 提供网络信息发送的
    /// </summary>

    class NetSender
    {
        public static int ServicePort = 888;//服务器端口就是要发
        public static string ServiceIp = "127.0.0.1";//服务器IP，由文件保存

        private UdpClient tcp = new UdpClient();

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendMessage(Message.Message msg)
        {
            try
            {
                tcp = new UdpClient();
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                serializer.Serialize(memStream, msg);
                byte[] bt = memStream.GetBuffer();
                memStream.Close();
                tcp.Connect(ServiceIp, ServicePort);

                tcp.Send(bt, bt.Length);
                tcp.Close();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static NetSender Sender = new NetSender();
    }

    /// <summary>
    /// 提供网络信息的读取服务
    /// </summary>
    class NetReciver
    {
        public static int port = 888;//固定端口888，就是发
        public static string ip;

        public static void InitialIp()
        {
            //IPHostEntry ip = new IPHostEntry();
            string HostName = Dns.GetHostName();
            //this.textBox1.Text = HostName;
            IPAddress[] MyEntry = Dns.GetHostAddresses(HostName);
            foreach (var r in MyEntry)
            {
                if (r.AddressFamily == AddressFamily.InterNetwork)
                    ip = r.ToString();
            }
        }

        public event Action<Message.Message> MessageArrived; //事件到达时的事件
        private UdpClient udp;

        public NetReciver()
        {
            InitialIp();
            IPAddress ipad;
            IPAddress.TryParse(ip,out ipad);
            udp = new UdpClient(port);
            Listen();
        }
        
        async private Task Listen()
        {
            for (; ; )
            {
                UdpReceiveResult rs = await udp.ReceiveAsync();
                Message.Message msg = parseByte(rs.Buffer);
                MessageArrived(msg);
                //Thread.Sleep(100);
            }
        }

        private Message.Message parseByte(byte[] buf)
        {
            System.IO.MemoryStream memStream = new MemoryStream(buf);
            memStream.Position = 0; 
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object newobj = deserializer.Deserialize(memStream); 
            memStream.Close();
            if (newobj is Message.Message)
            {
                return (Message.Message)newobj;
            }
            return null;
        }

        public static NetReciver Listener = new NetReciver();
        
    }
}
    